const os = require( 'os' );
const fs = require( 'fs' );
const path = require( 'path' );
const http = require( 'http' );
const filepath = path.join( __dirname, 'logs', gt() + '.log' );

const server = http.createServer();
server.on( 'request', ( req, res ) => { 
    console.log(req.headers);
    let tt = gt( 1 );
    let ip = req.socket.remoteAddress;
    let menthod = req.socket.RequestMethod;
    // let pathName=filepath.headers['']
    let userAgent = req.headers['user-agent'];
    let data=`${ip}-${tt}-${menthod}-${userAgent}${os.EOL}`
    fs.appendFile( filepath, data, err => { })
    res.setHeader( 'Content-Type', 'text/html;charset=utf8' );
    res.end( 'hello' );
})
server.listen( 8080, () => { 
    console.log('web启动');
})
function gt( flag=0) {
    const date = new Date();
    const yy = date.getFullYear();
    const mm = date.getMonth()+1;
    const dd = date.getDate();
    const hh = date.getHours();
    const mi = date.getMinutes();
    const se = date.getSeconds();
    if ( flag === 0 ) {
        return `${ yy }-${ mm }-${ dd }`;
    } else { 
        return `${ yy }-${ mm }-${ dd } ${ hh }:${ mi }:${ se }`;
    }
}